package com.springboot.cloud.demos.producer.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.demos.entity.form.UserForm;
import com.springboot.cloud.demos.entity.form.UserPositionForm;
import com.springboot.cloud.demos.entity.form.UserQueryForm;
import com.springboot.cloud.demos.entity.param.UserQueryParam;
import com.springboot.cloud.demos.entity.po.User;
import com.springboot.cloud.demos.producer.service.IUserService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Api("user")
@Slf4j
public class UserController {

    @Autowired
    private IUserService userService;

    @ApiOperation(value = "新增产品", notes = "新增一个产品")
    @ApiImplicitParam(name = "userForm", value = "新增产品form表单", required = true, dataType = "UserForm")
    @PostMapping
    public Result <User>  add(@Valid @RequestBody UserForm userForm) {
        log.info("name:{}", userForm);
        User user = userForm.toPo(User.class);
        boolean res = userService.add(user);
        return Result.success(user);
    }

    @ApiOperation(value = "删除产品", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(paramType = "path", name = "id", value = "产品ID", required = true, dataType = "long")
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable String id) {
        return Result.success(userService.delete(id));
    }

    @ApiOperation(value = "修改产品", notes = "修改指定产品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "产品ID", required = true, dataType = "long"),
            @ApiImplicitParam(name = "userForm", value = "产品实体", required = true, dataType = "UserForm")
    })
    @PutMapping(value = "/{id}")
    public Result<User> update(@PathVariable String id, @Valid @RequestBody User user) {
        return Result.success(userService.update(user));
    }

    @ApiOperation(value = "获取产品", notes = "获取指定产品信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "产品ID", required = true, dataType = "long")
    @GetMapping(value = "/{id}")
    public Result<User> get(@PathVariable String id) {
        log.info("get with id:{}", id);
        return Result.success(userService.get(id));
    }

    @ApiOperation(value = "查询产品", notes = "根据条件查询产品信息，简单查询")
    @ApiImplicitParam(paramType = "queryByOpenId", name = "openId", value = "产品名称", required = true, dataType = "string")
    @ApiResponses(
            @ApiResponse(code = 200, message = "处理成功", response = Result.class)
    )
    @GetMapping("/queryByOpenId")
    public Result<User> queryByOpenId(String openId) {
        UserQueryParam userQueryParam = new UserQueryParam();
        userQueryParam.setOpenId(openId);
        List<User> l = userService.query(userQueryParam);
        if (!l.isEmpty()) {
            return Result.success(l.get(0));
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "查询产品", notes = "根据条件查询产品信息，简单查询")
    @ApiImplicitParam(paramType = "queryByMobile", name = "openId", value = "产品名称", required = true, dataType = "string")
    @ApiResponses(
            @ApiResponse(code = 200, message = "处理成功", response = Result.class)
    )
    @GetMapping("/queryByMobile")
    public Result<User> queryByMobile(String mobile) {
        return Result.success(userService.queryByMobile(mobile));
    }

    @ApiOperation(value = "搜索产品", notes = "根据条件查询产品信息")
    @ApiImplicitParam(name = "userQueryForm", value = "产品查询参数", required = true, dataType = "UserQueryForm")
    @ApiResponses(
            @ApiResponse(code = 200, message = "处理成功", response = Result.class)
    )
    @PostMapping(value = "/conditions")
    public Result search(@Valid @RequestBody UserQueryForm userQueryForm) {
        log.info("search with userQueryForm:{}", userQueryForm);
        return Result.success(userService.query(userQueryForm.toParam(UserQueryParam.class)));
    }


    @ApiOperation(value = "用户相关统计（我喜欢的和喜欢我的人数", notes = "根据条件查询产品信息，简单查询")
    @GetMapping("/getStatistics")
    public Result getStatistics(String userId) {
        Map<String, Object> res = userService.getStatistics(userId);
        return Result.success(res);
    }

    @ApiOperation(value = "用户分页列表", notes = "根据条件查询产品信息，简单查询")
    @PostMapping("/nearBy")
    public Result<Page> nearBy(@RequestBody UserQueryForm userQueryForm) {
        IPage res = userService.nearBy(userQueryForm);
        return Result.success(res);
    }

    @ApiOperation(value = "用户分页列表", notes = "根据条件查询产品信息，简单查询")
    @PostMapping("/recommend")
    public Result<Page> recommend(@RequestBody UserQueryForm userQueryForm) {
        IPage res = userService.recommend(userQueryForm);
        return Result.success(res);
    }

    @ApiOperation(value = "喜欢我的用户分页列表", notes = "根据条件查询产品信息，简单查询")
    @PostMapping("/getFollowMe")
    public Result<Page> getFollowMe(@RequestBody UserQueryForm userQueryForm) {
        log.info("userQueryForm:{}",userQueryForm);
        IPage res = userService.getFollowMe(userQueryForm);
        return Result.success(res);
    }

    @ApiOperation(value = "我喜欢的用户分页列表", notes = "根据条件查询产品信息，简单查询")
    @PostMapping("/getMyFollow")
    public Result<Page> getMyFollow(@RequestBody UserQueryForm userQueryForm) {
        IPage res = userService.getMyFollow(userQueryForm);
        return Result.success(res);
    }
}

